home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Linux / SLAX 6.0.8 / slax-6.0.8.iso / slax / base / 006-devel.lzm / usr / include / arts / iomanager.h < prev    next >
Encoding:
C/C++ Source or Header  |  2005-09-10  |  5.6 KB  |  215 lines

  1.     /*
  2.  
  3.     Copyright (C) 2000 Stefan Westerfeld
  4.                        stefan@space.twc.de
  5.  
  6.     This library is free software; you can redistribute it and/or
  7.     modify it under the terms of the GNU Library General Public
  8.     License as published by the Free Software Foundation; either
  9.     version 2 of the License, or (at your option) any later version.
  10.   
  11.     This library is distributed in the hope that it will be useful,
  12.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14.     Library General Public License for more details.
  15.    
  16.     You should have received a copy of the GNU Library General Public License
  17.     along with this library; see the file COPYING.LIB.  If not, write to
  18.     the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  19.     Boston, MA 02111-1307, USA.
  20.  
  21.     */
  22.  
  23. /*
  24.  * BC - Status (2002-03-08):
  25.  *   BINARY COMPATIBLE: IONotify, TimeNotify, IOManager
  26.  *   NO BC FOR: StdIOManager
  27.  *
  28.  * Whereas the first three are part of the Interface (i.e. DEFINITELY to be
  29.  * kept binary compatible), the next three are part of the implementation
  30.  * in libmcop and subject to any kind of change.
  31.  */
  32.  
  33. #ifndef IOMANAGER_H
  34. #define IOMANAGER_H
  35.  
  36. #include <sys/time.h>
  37. #include <sys/types.h>
  38. #include <unistd.h>
  39. #include <list>
  40. #include <stack>
  41.  
  42. #include "arts_export.h"
  43.  
  44. namespace Arts {
  45. // constants:
  46.  
  47. /**
  48.  * What does the reentrant flag do?
  49.  *
  50.  * The IOManager offers a processOneEvent call. This means, that you can ask
  51.  * that I/O is handled, even while in a routine that handles I/O. As a
  52.  * practical example: you may have got a remote invocation for the function
  53.  * foo. Now you are in function foo() and call function bar() on a remote
  54.  * server. When you wait for the result, you obviously will again require
  55.  * the IOManager to wait for it. Thus this is a case where you need reentrant
  56.  * I/O handling.
  57.  *
  58.  * That way, you get a multiple level stack:
  59.  *
  60.  * <pre>
  61.  *    [...]
  62.  *      |
  63.  * [ Hander for I/O ]
  64.  *      |
  65.  * [ IOManager ]              level 2
  66.  *      |
  67.  * [ Some other function ]
  68.  *      |
  69.  * [ Hander for I/O ]
  70.  *      |
  71.  * [ IOManager ]              level 1
  72.  *      |
  73.  * [ main() ]
  74.  * </pre>
  75.  *
  76.  * What reentrant does, is to allow that IO Watch to be activated at levels
  77.  * higher than one.
  78.  *
  79.  * Timers and notifications, on the other hand will only be carried out at
  80.  * level 1.
  81.  */
  82. struct IOType {
  83.     enum { read = 1, write = 2, except = 4, reentrant = 8, all = 15 };
  84. };
  85.  
  86. /**
  87.  * IONotify is the base class you can derive from to receive callbacks about
  88.  * IO activity. You will need to call the watchFD function of the IOManager
  89.  * to start watching a filedescriptor.
  90.  *
  91.  * @see Arts::IOManager
  92.  */
  93. class ARTS_EXPORT IONotify {
  94. public:
  95.     /**
  96.      * This is the function that gets called if something relevant happened
  97.      * on the filedescriptor you watched with IOManager::watchFD.
  98.      *
  99.      * @param fd   is the filedescriptor that has seen some IO activity
  100.      * @param type is the type of activity (as combination of IOType)
  101.      */
  102.     virtual void notifyIO(int fd, int type) = 0;
  103. };
  104.  
  105. /**
  106.  * TimeNotify is the base class you can derive from to receive timer callbacks.
  107.  * You will need to call the addTimer function of the IOManager to start
  108.  * watching a filedescriptor.
  109.  *
  110.  * @see Arts::IOManager
  111.  */
  112. class ARTS_EXPORT TimeNotify {
  113. public:
  114.     /**
  115.      * This function gets whenever the timer is activated. Note that the
  116.      * IOManager will try to "catch up" lost time, that is, if you have a
  117.      * 300ms timer that didn't get called for a second (because there was
  118.      * something else to do), you'll receive three calls in a row.
  119.      */
  120.     virtual void notifyTime() = 0;
  121. };
  122.  
  123. /**
  124.  * Provides services like timers and notifications when filedescriptors get
  125.  * ready to read/write.
  126.  */
  127. class ARTS_EXPORT IOManager {
  128. public:
  129.     virtual ~IOManager() {};
  130.  
  131.     /**
  132.      * processes exactly one io event
  133.      */
  134.     virtual void processOneEvent(bool blocking) = 0;
  135.  
  136.     /**
  137.      * enters a loop which processes io events, until terminate is called
  138.      *
  139.      * may only be called once (use processOneEvent for other purposes)
  140.      */
  141.     virtual void run() = 0;
  142.  
  143.     /**
  144.      * terminates the io event loop (which was started with run)
  145.      */
  146.     virtual void terminate() = 0;
  147.  
  148.     /**
  149.      * starts watching one filedescriptor for certain types of operations
  150.      *
  151.      * notifies the notify object when e.g. the fd requires (allows) reading
  152.      * and types contained IOType::read.
  153.      *
  154.      * @see Arts::IOType
  155.      * @see Arts::IONotify
  156.      */
  157.     virtual void watchFD(int fd, int types, IONotify *notify) = 0;
  158.  
  159.     /**
  160.      * stops watching a filedescriptor
  161.      */
  162.     virtual void remove(IONotify *notify, int types) = 0;
  163.     /*
  164.      * BCI when breaking BC, probably int fd should be added as argument
  165.      * to remove, this would be more consistent with the way watches are added
  166.      */
  167.  
  168.     /**
  169.      * starts a periodic timer
  170.      *
  171.      * @see Arts::TimeNotify
  172.      */
  173.     virtual void addTimer(int milliseconds, TimeNotify *notify) = 0;
  174.  
  175.     /**
  176.      * stops the timer
  177.      */
  178.     virtual void removeTimer(TimeNotify *notify) = 0;
  179. };
  180.  
  181. class IOWatchFD;
  182. class TimeWatcher;
  183.  
  184. class ARTS_EXPORT StdIOManager : public IOManager {
  185. protected:
  186.     std::list<IOWatchFD *> fdList;
  187.     std::list<TimeWatcher *> timeList;
  188.     std::stack<IOWatchFD *> notifyStack;
  189.  
  190.     bool terminated;
  191.  
  192.     bool fdListChanged;    // causes the fd_sets to be rebuilt before using them
  193.     bool timeListChanged;
  194.     fd_set readfds, writefds, exceptfds;
  195.     fd_set reentrant_readfds, reentrant_writefds, reentrant_exceptfds;
  196.     int maxfd;
  197.  
  198.     int level;
  199.  
  200. public:
  201.     StdIOManager();
  202.  
  203.     void processOneEvent(bool blocking);
  204.     void run();
  205.     void terminate();
  206.     void watchFD(int fd, int types, IONotify *notify);
  207.     void remove(IONotify *notify, int types);
  208.     void addTimer(int milliseconds, TimeNotify *notify);
  209.     void removeTimer(TimeNotify *notify);
  210. };
  211.  
  212. }
  213.  
  214. #endif
  215.